package cn.nbt.busManage.controller;


import cn.nbt.busManage.common.ResponseResult;
import cn.nbt.busManage.model.dto.MenuDto;
import cn.nbt.busManage.model.dto.NewsDto;
import cn.nbt.busManage.model.vo.MenuVo;
import cn.nbt.busManage.model.vo.NewsVo;
import cn.nbt.busManage.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping("/list")
    public ResponseResult<Object> findAll(){
        ResponseResult<Object> result = new ResponseResult<>();
        try {
            List<MenuDto> all = menuService.findAll();
            ArrayList<MenuVo> menuVos = new ArrayList<>();
            for (MenuDto menuDto:all){
                MenuVo menuVo = new MenuVo(menuDto);
                menuVos.add(menuVo);
            }
            result.Success("查询成功",menuVos);
        }catch (Exception e){
            result.Error("失败了！",e.getMessage());
        }
        return result;
    }

    @GetMapping("/findById")
    public ResponseResult<Object> findById(@RequestParam(name = "id") Integer id){
        ResponseResult<Object> result = new ResponseResult<>();
        try {
            MenuDto menuDto = menuService.findById(id);
            MenuVo menuVo = new MenuVo(menuDto);
            result.Success("查询成功",menuVo);
        }catch (Exception e){
            result.Error("失败了！",e.getMessage());
        }
        return result;
    }

    @GetMapping("/findByQuery")
    public ResponseResult<Object>findByQuery(@RequestParam(name="name") String name){
        ResponseResult <Object> result=new ResponseResult<>();
        try {
            List<MenuDto> menuDtolist=menuService.findByQuery(name);
            if (menuDtolist == null){
                result.BadRequest();
                return result;
            }
            ArrayList<MenuVo> menuVos = new ArrayList<>();
            for (MenuDto menuDto :menuDtolist) {
                menuVos.add(new MenuVo(menuDto));
            }
            result.Success("查询成功",menuVos);
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error("查询失败");
        }
        return result;
    }

    @PostMapping("/save")
    public ResponseResult<Object> save(@RequestBody MenuVo menuVo){
        ResponseResult<Object> result = new ResponseResult<>();
        try {
            MenuDto menuDto = new MenuDto(menuVo);
            menuService.save(menuDto);
            result.Success("保存成功");
        }catch (Exception e){
            result.Error("失败了！",e.getMessage());
        }
        return result;
    }

    @DeleteMapping("/delete")
    public ResponseResult<Object> deleteById(@RequestParam(name = "id") Integer id){
        ResponseResult<Object> result = new ResponseResult<>();
        try {
            menuService.deleteById(id);
            result.Success("删除成功");
        }catch (Exception e){
            result.Error("失败了！",e.getMessage());
        }
        return result;
    }

    @PutMapping("/update")
    public ResponseResult<Object> update(@RequestParam(name = "menu") MenuVo menuVo){
        ResponseResult<Object> result = new ResponseResult<>();
        try {
            MenuDto menuDto = new MenuDto(menuVo);
            menuService.update(menuDto);
            result.Success("更新成功");
        }catch (Exception e){
            result.Error("失败了！",e.getMessage());
        }
        return result;
    }
}
